/*
 * This file will contain all the functions pertaining to handling
 * user input (thread 1)
 */

#include "input.h"


/*
 * This function will handle user input
 * @param threads, the array containing each thread
 */
void *handleInput(void *notUsed)
{
	char *prompt = "mftp> ";
	char input[INPUT_LEN]; //user input
	char *strPtr;  //used for strtok_r
	char *command, *arg;  


	//flush stdin
	int c;
	while ((c = getchar()) != '\n' && c != EOF);


	while(!shouldExit)
	{

	//	updatePrompt(prompt);
		pthread_mutex_unlock(&mT0AndT1);


		//print prompt
		printf("%s", prompt);


		//wait for command
		fgets(input, INPUT_LEN - 2, stdin);


		command = strtok_r(input, " \t\n", &strPtr);

		if(!command)
			continue;

		arg = strtok_r(NULL, " \t\n", &strPtr);

		handleCommand(command, arg);



	}

	if(pthread_join(threads[1], NULL) ||
			pthread_join(threads[2], NULL))
	{
		perror("Failed to join threads");
		exit(1);
	}

	pthread_exit(NULL);

	return NULL;
}

void handleCommand(char *cmd, char *arg)
{
	//will hold the ftp command
	char *ftpCmd;

	//message to be sent to server
	char msg[INPUT_LEN];

	//tests if the command has an argument
	bool hasArg = false;

	//if a command needs the data socket, this will containt the command for t2
	char *dataCmd = NULL;

	if(!strcmp("exit", cmd))
	{
		pthread_mutex_lock(&mShouldExit);
		shouldExit = true;
		pthread_mutex_unlock(&mShouldExit);
		pthread_cond_signal(&t0AndT1BufRdy);
		pthread_cond_signal(&listBufRdy);

		return;
	}
	else if(!strcmp("cd", cmd))
	{
		ftpCmd = "CWD";
		hasArg = true;
	}
	else if(!strcmp("pwd", cmd))
	{
		ftpCmd = "PWD";
	}
	else if(!strcmp("ls", cmd))
	{
		ftpCmd = "LIST";
		hasArg = true;
		needsDataSock = true;

		dataCmd = "l";

	}
	else if(!strcmp("get", cmd))
	{
		ftpCmd = "RETR";
		hasArg = true;
		needsDataSock = true;

		char tmpStr[INPUT_LEN];

		sprintf(tmpStr, "d %s", arg);
		dataCmd = tmpStr;

	}
	else{
		printf("Command not recognized\n");
		return;
	}

	//if there is an argument
	if(hasArg && arg)
	{
		sprintf(msg, "%s %s\r\n", ftpCmd, arg);
	}
	//else send with no argument
	else
	{
		sprintf(msg, "%s\r\n", ftpCmd);
	}
	communicateWithT1(msg);

	//if data socket is needed, communicate with thread 2
	if(dataCmd && t0AndT1Buf->code == C_DATARDY)
		communicateWithT2(dataCmd);
}

/*
 *Sends and receives ftp commands and responses to and from thread 1
 This function also prints out the responses
 @param msg: the ftp command
 */
void communicateWithT1(char *msg)
{

	pthread_mutex_lock(&mT0AndT1);

	if(t0AndT1Buf){
		freeResp(&t0AndT1Buf);
	}
	t0AndT1Buf = newResp(-1, msg);

	pthread_cond_signal(&t0AndT1BufRdy);
	pthread_cond_wait(&t0AndT1BufRdy, &mT0AndT1); //wait for response from thread 1

	printf("%s", t0AndT1Buf->message);
	pthread_mutex_unlock(&mT0AndT1);

}

/*
 *If msg is "l", get directory listing
 msg starts with "d", download a file
 msg starts with "u", upload a file
 */
void communicateWithT2(char *msg)
{
	listBuf = msg;

	pthread_cond_signal(&listBufRdy);



	//if downloading a file
	if(msg[0] == 'd')
	{


		printf("Downloading...\n");

	}
	//else ls command was used.  print out the list
	else{
		pthread_cond_wait(&listBufRdy, &mListBuf);
		printf("%s", listBuf);	
		free(listBuf);
		listBuf = NULL;
		pthread_mutex_unlock(&mListBuf);
	}

	pthread_cond_wait(&t0AndT1BufRdy, &mT0AndT1);


}

//updates the prompt so it has the current working directory
void updatePrompt(char *prompt)
{

	sleep(1);
	char *strPtr; //used for strtok_r
	pthread_mutex_lock(&mT0AndT1);

	if(t0AndT1Buf){
		freeResp(&t0AndT1Buf);
	}
	t0AndT1Buf = newResp(-1, "PWD\r\n");

	pthread_cond_signal(&t0AndT1BufRdy);
	pthread_cond_wait(&t0AndT1BufRdy, &mT0AndT1); //wait for response from thread 1
	pthread_mutex_unlock(&mT0AndT1);

	strtok_r(t0AndT1Buf->message, "\"", &strPtr);
	strcpy(prompt, strtok_r(NULL, "\"", &strPtr));

	strcat(prompt, "> ");



}



